//
//  StatusViewModel.swift
//  weibo
//
//  Created by 吴婷婷 on 15/11/10.
//  Copyright © 2015年 wutingting. All rights reserved.
//

import UIKit

class StatusVewModel: CustomStringConvertible {
    ///微博模型
    var status: Status
    ///缓存行高
    lazy var rowHeight: CGFloat = {
        
        let cell: StatusCell
    
        if self.status.retweeted_status == nil {
            cell = StatusNormalCell(style: .Default, reuseIdentifier: StatusNormalCellId)
        } else {
            cell = StatusRetweetedCell(style: .Default, reuseIdentifier: StatusRetweetedCellId)
        }
        
        return cell.rowHeight(self)
        
    }()
    
    ///用户头像地址
    var userProfileUrl: NSURL {
        return NSURL(string: status.user?.profile_image_url ?? "")!
    }
    ///默认头像
    var userDefaultIconView: UIImage {
        return UIImage(named: "avatar_default_big")!
    }
    ///用户会员图标
    var userMemberImage: UIImage? {
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            // TODO: ！与 ？
            return UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
        }
        return nil
    }
    
    /// 用户认证图标
    /// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    var userVipImage: UIImage? {
        switch(status.user?.verified_type ?? -1){
        case 0: return UIImage(named: "avatar_vip")
        case 2,3,5: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default: return nil
        }
        
    }
    ///转发微博文字
    var retweetedText: String? {
        
        guard let s = status.retweeted_status else {
            return nil
        }
        
        return "@" + (s.user?.screen_name ?? "") + ":" + (s.text ?? "")
    }
    ///可重用标示
    var cellId: String {
        return status.retweeted_status == nil ? StatusNormalCellId: StatusRetweetedCellId
    }
    ///缩略图URL数组
    var thumbnail: [NSURL]?
    
    init(status: Status){
        self.status = status
        
        if let urls = status.retweeted_status?.pic_urls ?? status.pic_urls {
            
            thumbnail = [NSURL]()
            for dict in urls {
                let url = NSURL(string: dict["thumbnail_pic"]!)
                thumbnail?.append(url!)
            }
        }
    }
    ///描述信息
    var description: String {
        return status.description + "\n配图数组:\(thumbnail ?? [] as NSArray)"
    }
}